package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetPermissionListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取权限列表
func NewGetPermissionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPermissionListLogic {
	return &GetPermissionListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetPermissionListLogic) GetPermissionList(req *types.GetPermissionListRequest) (resp *types.GetPermissionListResponse, err error) {
	reply, err := l.svcCtx.AuthRoleRpc.GetPermissionList(l.ctx, &v1.GetPermissionListRequest{
		Page:                 req.Page,
		PageSize:             req.PageSize,
		PermissionPid:        req.PermissionPid,
		PermissionName:       req.PermissionName,
		PermissionIdentifier: req.PermissionIdentifier,
		Status:               v1.Status(req.Status),
	})
	if err != nil {
		return resp, err
	}

	resp = &types.GetPermissionListResponse{ResponsePagination: types.ResponsePagination{
		Page:      reply.GetPagination().GetPage(),
		PageSize:  reply.GetPagination().GetPageSize(),
		TotalPage: reply.GetPagination().GetTotalPage(),
		Total:     reply.GetPagination().GetTotal(),
	}}

	for _, permission := range reply.List {
		resp.List = append(resp.List, types.PermissionList{
			Id:                   permission.GetId(),
			PermissionPid:        permission.GetPermissionPid(),
			PermissionName:       permission.GetPermissionName(),
			PermissionIdentifier: permission.GetPermissionIdentifier(),
			ListOrder:            permission.GetListOrder(),
			Status:               int8(permission.GetStatus()),
			CreatedAt:            permission.GetCreatedAt(),
			UpdatedAt:            permission.GetUpdatedAt(),
		})
	}

	return resp, err
}
